<?php
class FollowAction extends Action{


	public function doFollow(){//【添加关注】
		$uid = $_POST['uid'];
		$fid = $_POST['fid'];
		$follow = model('Follow');
		$user_follow = M('User_follow');
		$is_remark  =$user_follow->where("uid = '$uid' and fid = '$fid' and remark = '1'")->find();
		if($is_remark){
			$update['remark'] = 0;
			$data = $user_follow->where("uid = '$uid' and fid = '$fid'")->data($update)->save();
			if($data){
				$data = $follow->getFollowCount($uid);
				$value = ['code'=>'success','data'=>$data];
				header("Content-Type: text/json");
				die(json_encode($value));
			}else{
				$value = ['code'=>'false'];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
		}
		
		$is_follow = $user_follow->where("uid = '$uid' and fid = '$fid'  and remark = '0'")->find();
		if($is_follow){//已关注
			$value = ['code'=>'isfollow'];
			header("Content-Type: text/json");
			die(json_encode($value));
		
		}
		
		$data = $follow->doFollow($uid,$fid);
		if($data){//关注成功
			$data = $follow->getFollowCount($uid);
			$value = ['code'=>'success','data'=>$data];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{//关注失败
		
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	public function unFollow(){//【取消关注】
		$uid = $_POST['uid'];
		$fid = $_POST['fid'];
		$follow = model('Follow');
		$find = M('User_follow')->where("uid = '$uid' and fid ='$fid' and is_friend = '1'")->find();
		if($find){
			$update['remark'] = 1;
			$result = M('User_follow')->where("uid = '$uid' and fid ='$fid'")->data($update)->save();
			if($result){
				$data = $follow->getFollowCount($uid);
				$value = ['code'=>'success','data'=>$data];
				header("Content-Type: text/json");
				die(json_encode($value));
			}else{
		
				$value = ['code'=>'false'];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
		}
		$data = $follow->unFollow($uid,$fid);
		if($data){
			$data = $follow->getFollowCount($uid);
			$value = ['code'=>'success','data'=>$data];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
		
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	public function getFollowCount() {//【获取用户关注数和粉丝数】
		$uid = $_POST['uid'];
		$uids = array($uid);
		$follow = model('Follow');
		$count = $follow->getFollowCount($uids);
		if($count){
			$value = ['code'=>'success','data'=>$count];
			dump($value);exit;
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
		
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	public function isFollow($uid,$fid){//【是否已关注】
		$uid = $_POST['uid'];
		$fid = $_POST['fid'];
		$follow = M('User_follow');
		$result = $follow->where("uid = $uid and fid = $fid and remark = 0")->find();
		if($result){
			$value = ['code'=>'success'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}else{
		
			$value = ['code'=>'false'];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}